package com.wing.bill.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.bill.dao.PurchaseBillDao;
import com.wing.bill.dao.PurchaseBillReceiptDao;
import com.wing.bill.entity.PurchaseBill;
import com.wing.bill.entity.PurchaseBillReceipt;
import com.wing.common.exception.BusinessException;
import com.wing.product.service.DynamicService;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 采购账单发票
 *
 * @author xiayang
 */
@Service
@Slf4j
public class PurchaseBillReceiptServiceImpl extends ServiceImpl<PurchaseBillReceiptDao, PurchaseBillReceipt> implements PurchaseBillReceiptService {

    @Resource
    DynamicService dynamicService;

    @Override
    public int add(PurchaseBillReceipt purchaseBillReceipt) {
        if (purchaseBillReceipt.getBillId() == null) {
            throw new BusinessException("账单信息不能为空");
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String code = dynamicService.getMaxCode("code", "pur_purchase_bill_receipt");
        String day = simpleDateFormat.format(new Date());
        purchaseBillReceipt.setCode("KP" + day + code);
        purchaseBillReceipt.setCreator(JwtUtils.getLoginUserId());
        purchaseBillReceipt.setCreatorName(JwtUtils.getLoginUsername());
        purchaseBillReceipt.setCreateTime(LocalDateTime.now());
        return this.baseMapper.insert(purchaseBillReceipt);
    }

    @Override
    public List<PurchaseBillReceipt> list(Map<String, Object> params) {
        return this.baseMapper.list(params);
    }

    @Override
    public Page<PurchaseBillReceipt> pageList(Page page, Map<String, Object> params) {
        List<PurchaseBillReceipt> list = baseMapper.list(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public PurchaseBillReceipt get(Long id) {
        return this.baseMapper.get(id);
    }
}
